import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * AddBudgetWindow.java
 * 
 * Provides a window to add a student to the database
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AddBudgetWindow extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = -1987273009239489086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	

	private JPanel pane;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JTextField availableInput;
	private AddBudgetWindow self = this;
	private boolean added = false;
	
	/**
	 * Constructor
	 */
	public AddBudgetWindow(JFrame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Add Budget");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		
		availableInput = new JTextField("Available", 20);
		
		selectBut = new JButton("Add");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		pane.add(availableInput, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method for ActionListener
	 * Called by Select/Cancel Buttons
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut && availableInput.getText().length() > 0) {
			try {
				Double.parseDouble(availableInput.getText());
				added = true;
				this.setVisible(false);
			}
			catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(self, "Available must be a valid amount\nExample: 2000.00", 
						"Budget Addtion Warning", JOptionPane.WARNING_MESSAGE);
			}
		}
		else if (e.getSource() == selectBut)
			JOptionPane.showMessageDialog(self, "Available field must have a value", 
					"Budget Addition Warning", JOptionPane.WARNING_MESSAGE);
		else if (e.getSource() == cancelBut)
			this.setVisible(false);
	}
	
	/**
	 * Returns the information for the new Student
	 * 
	 * @return	An array of information for new Student
	 */
	public String getResult() {
		if(added) {
			return availableInput.getText();
		}
		else 
			return null;
	}
}
